package com.woniuxy.controller;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Role;
import com.woniuxy.entity.User;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.service.RbacRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName RbacRoleController
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/3 14:01
 * @Version 1.0
 */
@RequestMapping("/rbacRole")
@RestController
@CrossOrigin
public class RbacRoleController {
    @Autowired
    RbacRoleService roleService;

    @RequestMapping("/addOne")
    @RequiresPermissions({"role:addOne","admin"})
    public ResponseEntity addOne(@RequestBody Role role){
        List<Role> list= findUserByName(role.getName());
        if(list.size()!=0){
            return new ResponseEntity(500,"用户名重复",null);
        }else {
            return roleService.addRole(role);
        }
    }
    @GetMapping("/")
    @RequiresPermissions({"role:findAll","admin"})
    public ResponseEntity findAll() {
        return roleService.selectAll();
    }
    @RequestMapping("/editOne")
    @RequiresPermissions({"role:editOne","admin"})
    public ResponseEntity editOne(@RequestBody Role role){
        return roleService.editOne(role);
    }
    @GetMapping("/search/{currentPage}/{pageSize}")
    public ResponseEntity search(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize,@RequestParam String name){
        return roleService.search(currentPage,pageSize,name);
    }
    @RequestMapping("/deleteById")
    @RequiresPermissions({"role:deleteById","admin"})
    public ResponseEntity deleteById(String id){
        return roleService.deleteRoleById(Integer.parseInt(id));
    }
    public List<Role> findUserByName(String username){
        List<Role> list = roleService.findRoleByName(username);
        return list;
    }
}
